package com.teradata.olingo.service;

import com.teradata.olingo.entities.ApiMetadata;
import com.teradata.olingo.entities.EdmTypeTrnsf;
import com.teradata.olingo.mapper.ApiMetadataMapper;
import com.teradata.olingo.mapper.EdmTypeTrnsfMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EdmTypeTrnsfService {
    @Autowired
    private EdmTypeTrnsfMapper edmTypeTrnsfMapper;

    @Autowired
    private ApiMetadataMapper apiMetadataMapper;

    public List<EdmTypeTrnsf> getEdmTypeTrnsf(){
         return edmTypeTrnsfMapper.selectAll();
    }



    public List<ApiMetadata> getApiMetadata(String apiName){
         return apiMetadataMapper.selectByApiName(apiName);
    }


    public List<ApiMetadata> getAllApiMetadata(){
        return apiMetadataMapper.selectAll();
    }


    public Map<String, List<ApiMetadata>> getMetadataFromDB() {
        Map<String, List<ApiMetadata>> map = new HashMap<String, List<ApiMetadata>>();

        //List<ApiMetadata> allApiMetadata = edmTypeTrnsfService.getAllApiMetadata();
        List<ApiMetadata> allApiMetadata = apiMetadataMapper.selectAll();

        for (ApiMetadata apiMetadatum : allApiMetadata) {
            if (!(map.containsKey(apiMetadatum.getApiName()))) {
                //System.out.println("apiName:" + apiMetadatum.getApiName());
                map.put(apiMetadatum.getApiName(), new ArrayList<>(Arrays.asList(apiMetadatum)));
            } else {
                List<ApiMetadata> apiMetadataList = map.get(apiMetadatum.getApiName());
                apiMetadataList.add(apiMetadatum);
                map.put(apiMetadatum.getApiName(), apiMetadataList);
            }
        }

        return map;
    }
}
